﻿using System;
using System.Threading;
namespace ConsoleApp1
{
    using yunio;
    using yunio.websocket;
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            using var ws = YunioWebSocket.Listen("ws://localhost/ws/GetSyncMsg", YunioParam.New("key", "hello"), On_Message, cfg => cfg.SetAutoPingInterval(10));

            lock (ws) while (ws.IsConnected)
                    Monitor.Wait(ws, 1000);
        }

        public static void On_Message(YunioData data)
        {
            if (data.DataType == YunioDataType.Text) Console.WriteLine(data.Text);
            if (data.DataType == YunioDataType.Json)
            {
                if (data.Json.Text("type") == "heartbeat") return;
                if (data.Json.Text("type") == "welcome") Console.WriteLine(data.Json.Text("message"));
                else
                {
                    Console.WriteLine("----");
                    Console.WriteLine($"msg id: {data.Json.Text("msg_id")}");
                    Console.WriteLine($"from: {data.Json.Text("from_user_name.str")}");
                    Console.WriteLine(data.Json.Text("content.str"));
                    return;
                }
            }

            Console.WriteLine(data.Text);
        }
    }
}
